package com.vlong.town.controller.admin.insurance;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.insurance.InsuranceInfo;
import com.vlong.town.service.insurance.InsuranceInfoService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.pagination.PageTag;

/**
 * 保险
 * @author 51088
 *
 */
@Controller
@RequestMapping("/insurance")
public class InsuranceController extends BaseController{

	@Autowired
	private InsuranceInfoService  insuranceInfoService;//业务接口
	
	/**
	 * 保险列表查询
	 * @param pageNo
	 * @param pageSize
	 * @param title
	 * @param desc
	 * @param insuranceType
	 * @return String
	 */
	@RequestMapping("/list")
	public  String   list(
			@RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="title") String title,
            @RequestParam(required=false, value="desc") String desc,
            @RequestParam(required=false, value="insuranceType") String insuranceType
			){
		  	PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
	        PageHelper.orderBy("update_time DESC");
	        List<InsuranceInfo> queryList = insuranceInfoService.queryList(insuranceType, title, null, desc);
	        PageInfo<InsuranceInfo> pageInfo = new PageInfo<InsuranceInfo>(queryList);
	        PageTag<InsuranceInfo> pt = new PageTag<InsuranceInfo>(request, "list", pageInfo);
	        request.setAttribute("pageHtml", pt.getPageHtml());
	        request.setAttribute("title", title);
	        request.setAttribute("desc", desc);
	        request.setAttribute("insuranceType", insuranceType);
	        for(InsuranceInfo insuranceInfo : queryList) {
	        	insuranceInfo.setContent(DataUtil.delHTMLTag(insuranceInfo.getContent(), 0));
	        }
	        request.setAttribute("insuranceInfoList", queryList);
		return  "admin/insurance/list_insurance";
	}
	
	/**
	 * 跳转保险添加页面
	 * @return
	 */
	@GetMapping("/addInsurance")
	public   String  addInsurance(){
		return  "admin/insurance/add_insurance";
	}
	
	
	 /**
     * 添加保险信息
     * @param title 保险标题
     * @param content 保险内容
     * @param desc 保险简介
     * @param picture 缩略图
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeAddInsurance")
    public DataMessage addInsurance(
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture,
            @RequestParam(value="insuranceType",required=true) String insuranceType) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("保险标题不合法");
            return dm;
        }
        insuranceInfoService.add(1L, insuranceType, title, content, desc, picture);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("保险添加成功");
        return dm;
    }
	
    
    /**
     * 进入保险信息修改页面
     * @return notice/edit_notice
     */
    @GetMapping("/editInsurance/{insuranceId}")
    public String gotoNoticeInofEditPage(@PathVariable(required=true, value="insuranceId") Long insuranceId) {
    	InsuranceInfo insuranceInfo = insuranceInfoService.queryById(insuranceId);
    	request.setAttribute("insuranceInfo", insuranceInfo);
        request.setAttribute("thumbnail",  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + insuranceInfo.getThumbnail());
        System.out.println(request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + insuranceInfo.getThumbnail());
        return "admin/insurance/edit_insurance";
    }
    
    
    /**
     * 修改保险信息
     * @param title 保险标题
     * @param content 保险内容
     * @param desc 保险简介
     * @param picture 缩略图
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeEditInsurance")
    public DataMessage editNotice(
            @RequestParam(name="insuranceId", required=true) Long insuranceId,
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture,
            @RequestParam(value="insuranceType",required=true) String insuranceType
    		) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("保险标题不合法");
            return dm;
        }
        insuranceInfoService.update(insuranceId, insuranceType, title, content, desc, picture);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("保险修改成功");
        return dm;
    }
    
    /**
     * 删除保险
     * @param noticeId 主键值
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/delete")
    public DataMessage deleteUser(@RequestParam(required=true, value="insuranceId") Long insuranceId) {
        DataMessage dm = new DataMessage();
        if(insuranceId != null && insuranceId > 0L) {
        	insuranceInfoService.delete(insuranceId);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("保险删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }
    
    
    /**
     * 批量删除保险
     * @param noticeIds 主键值集合字符串
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/batchDelete")
    public DataMessage batchDeleteUser(@RequestParam(required=true, value="insuranceIds") String insuranceIds) {
        DataMessage dm = new DataMessage();
        if(this.validateStringProperty(insuranceIds, 1, 255)) {
            String[] insuranceIdArray = insuranceIds.split(",");
            for(String insuranceId : insuranceIdArray) {
            	insuranceInfoService.delete(Long.parseLong(insuranceId));
            }
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("保险删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }
    
	
}
